package com.allwees.bs.module.fms.entity;

import com.baomidou.mybatisplus.annotation.TableName;
import com.allwees.bs.core.modelbase.branch.EBranchType;
import com.allwees.core.common.constant.EWithdrawAccountType;
import com.allwees.core.common.util.MoneyUtil;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.Assert;

import javax.persistence.Column;
import java.math.BigDecimal;

/**
 * 商户账户
 *
 * @author yangylsky
 * @since 2019-12-11
 *
 */
@Data
@ToString
@Accessors(chain = true)
@EqualsAndHashCode(callSuper = true)
@TableName("fms_merchant_account")
public class MerchantAccount extends BaseAccount {
	private EBranchType branchType;



	private Integer accountType;

	public  String getAccountTypeDesc(){
		return  EWithdrawAccountType.getDescByAccountType(accountType);
	}


	/**
	 * 提现中金额
	 */
    @Column(precision = 8, scale = 2)
	private BigDecimal withdrawingAmount = BigDecimal.ZERO;

	/**
	 * 已提现金额
	 */
    @Column(precision = 8, scale = 2)
	private BigDecimal withdrawedAmount = BigDecimal.ZERO;

	/**
	 * 在途金额
	 */
	@Column(precision = 8, scale = 2)
	private BigDecimal  inShippingAmt = BigDecimal.ZERO;


	/**
	 * 待结算金额
	 */
	@Column(precision = 8, scale = 2)
	private BigDecimal  unsettleShippingAmt = BigDecimal.ZERO;


	/**
	 * 待发总金额
	 */
	@Column(precision = 8, scale = 2)
	private BigDecimal  waitingShipAmt = BigDecimal.ZERO;

	/**
	 * 退款总金额
	 */
	@Column(precision = 8, scale = 2)
	private BigDecimal  refundAmt = BigDecimal.ZERO;

	/**
	 * 银行卡数
	 */
	@Column
	private Integer bankNum;


	/**
	 * 账户名
	 */
	private  String accountName;


    /**
     * 按结算单执行结算
     *
     * @param settleStatement
     */
	/*public void doSettle(BaseSettleStatement settleStatement) {
		settledAmount = MoneyUtil.add(settledAmount, settleStatement.getAmount());
		unsettleShippingAmt = MoneyUtil.sub(unsettleShippingAmt,settleStatement.getAmount());
		balance = MoneyUtil.add(balance, settleStatement.getAmount());
	}*/


	/*public void calculateUnSettle(OmsOrderItem omsOrderItem) {
		BigDecimal amountTotal = BigDecimal.ZERO;
		BigDecimal skuAmount = MoneyUtil.mul(omsOrderItem.getPrice(), BigDecimal.valueOf(omsOrderItem.getQuantity()));
		BigDecimal skuShippingAmount = MoneyUtil.mul(omsOrderItem.getShippingPrice(), BigDecimal.valueOf(omsOrderItem.getQuantity()));
		amountTotal = MoneyUtil.add(amountTotal, skuAmount,skuShippingAmount);

		inShippingAmt =  MoneyUtil.sub(inShippingAmt,amountTotal);//发货后的在途总金额
		unsettleShippingAmt = MoneyUtil.add(unsettleShippingAmt, amountTotal);
	}

	public  void calculateWaitingShipAmt(OrderItem orderItem ) {
		waitingShipAmt =  MoneyUtil.add(waitingShipAmt,orderItem.getMerchantAmtSku());//待发总金额
	}

	public  void calculateRefundedAmt(OrderItem orderItem ) {
		BigDecimal amountTotal = BigDecimal.ZERO;
		BigDecimal skuAmount = MoneyUtil.mul(orderItem.getPrice(), BigDecimal.valueOf(orderItem.getQuantity()));
		BigDecimal skuShippingAmount = MoneyUtil.mul(orderItem.getShippingPrice(), BigDecimal.valueOf(orderItem.getQuantity()));
		amountTotal = MoneyUtil.add(amountTotal, skuAmount,skuShippingAmount);
		refundAmt =  MoneyUtil.add(refundAmt,amountTotal);//待发总金额
	}


	public  void calculateInShippingAmt(OmsOrderItem omsOrderItem){
		BigDecimal amountTotal = BigDecimal.ZERO;
		BigDecimal skuAmount = MoneyUtil.mul(omsOrderItem.getPrice(), BigDecimal.valueOf(omsOrderItem.getQuantity()));
		BigDecimal skuShippingAmount = MoneyUtil.mul(omsOrderItem.getShippingPrice(), BigDecimal.valueOf(omsOrderItem.getQuantity()));
		amountTotal = MoneyUtil.add(amountTotal, skuAmount,skuShippingAmount);
		waitingShipAmt =  MoneyUtil.sub(waitingShipAmt,amountTotal);//待发总金额
		inShippingAmt =  MoneyUtil.add(inShippingAmt,amountTotal);//发货后的在途总金额
	}*/



	/**
	 * 按指定币种执行提现操作（扣除了提现金额和手续费）
	 *
	 * @param amount
	 * @param
	 * @return
	 */
	@Override
	public WithdrawResult applyWithdraw(BigDecimal amount, String bsCurrency) {
		WithdrawResult withdrawResult = super.applyWithdraw(amount, bsCurrency);
		withdrawingAmount = withdrawingAmount.add(withdrawResult.getWithdrawAmount());
		return withdrawResult;
	}

	/**
	 * 按指定币种执行提现操作（扣除了提现金额和手续费）
	 *
	 * @param amount
	 * @param
	 * @return
	 */
	@Override
	public WithdrawResult applyWithdraw2(BigDecimal amount, String bsCurrency,String commissionRate, String fee ) {
		WithdrawResult withdrawResult = super.applyWithdraw2(amount, bsCurrency,commissionRate,fee);
		withdrawingAmount = withdrawingAmount.add(withdrawResult.getAmount());
		return withdrawResult;
	}


	/**
	 * 执行提现
	 *
	 * @param amount
	 * @param bsCurrency
	 * @return
	 */
	public void doWithdraw(BigDecimal amount, String bsCurrency) {
		assertCanDoWithdraw(amount, bsCurrency);
		withdrawingAmount = withdrawingAmount.subtract(amount);
		withdrawedAmount = withdrawedAmount.add(amount);
	}


	/**
	 * 执行失败
	 *
	 * @param amount
	 * @param bsCurrency
	 * @return
	 */
	public void doWithdrawFailed(BigDecimal amount, String bsCurrency) {
		assertCanDoWithdraw(amount, bsCurrency);
		withdrawingAmount = withdrawingAmount.subtract(amount);
		balance = balance.add(amount);
		settledAmount = settledAmount.add(amount);
	}

	/**
	 * 拒绝提现申请
	 *
	 * @param amount
	 * @param bsCurrency
	 * @return
	 */
	public void rejectWithdraw(BigDecimal amount, BigDecimal fee, String bsCurrency) {
		assertCanDoWithdraw(amount, bsCurrency);
		withdrawingAmount = withdrawingAmount.subtract(amount);
		settledAmount = settledAmount.add(amount).add(fee);
		balance = balance.add(amount).add(fee);
	}

	public void assertCanDoWithdraw(BigDecimal amount, String bsCurrency) {
		Assert.isTrue(StringUtils.isNotBlank(bsCurrency), "币种不能为空");
		Assert.isTrue(MoneyUtil.biggerThan(withdrawingAmount, amount) || MoneyUtil.equals(withdrawingAmount, amount), "提现中余额不足");
	}
}
